<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/head.png" alt="" />
        <span>Vue后台</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      
      <el-aside :width="isCollapse ?'64px':'200px'">
        <div class="toggle-button" @click="toggle">|||</div>
        <el-menu
          background-color="#393D49"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="$route.path"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.catname}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+ subItem.path" v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.catname}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <!-- 路由佔位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconObj: {
        '1':'iconfont icon-ziyuanxhdpi',
        '6':'iconfont icon-quanxian',
        '8':'iconfont icon-wodeshangpin',
        '10':'iconfont icon-5',
        '12':'iconfont icon-shujutongji',
      },
      isCollapse:false
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    loginout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    getMenuList() {
      this.$http.get('/menu').then( set => {
        const res = set.data;
        if(res.meta.status !==200 ){return this.$msg.error(res.meta.msg);}
        this.menulist = res.data;
        console.log(this.menulist);
      });
    },
    // 折叠收起菜单栏
    toggle(){
      this.isCollapse = !this.isCollapse
    },
  },
}
</script>

<style Lang="less">
.iconfont{
  margin-right: 10px;
}
.home-container {
  height: 100%;
}
.el-header {
  background-color: #23262e;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div span {
  margin-left: 15px;
}
.el-aside {
  background-color: #393d49;
}
.el-aside .el-menu{
  border: none;
}
.el-main {
  background-color: #fff;
}
.toggle-button{
  background-color: #4a5064;
  text-align: center;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  letter-spacing: 0.2rem;
  cursor: pointer;
}
</style>